{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "044ed8c0-3026-47ca-bd34-932e4f479ae9",
   "metadata": {},
   "source": [
    "# 智能体Agent机器人动作编排\n",
    "\n",
    "同济子豪兄 2024-8-31"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1431ceb-704c-4223-8a1a-a7b993ada48a",
   "metadata": {},
   "source": [
    "## 导入工具包和模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "09487884-4518-4a80-8264-2da76bc8d6cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "导入工具包\n",
      "开始载入phi3.5-mini-instruct-openvino-int4模型\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling the model to GPU ...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型成功载入\n"
     ]
    }
   ],
   "source": [
    "from utils_phi35 import *"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "372cf3c6-9c27-4bfa-9006-35f17a51faf0",
   "metadata": {},
   "source": [
    "## 提示词模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5fc89fd4-130d-40d6-b900-7c0af20d298a",
   "metadata": {},
   "outputs": [],
   "source": [
    "robot_order_template = '''\n",
    "请你根据我的指令，以列表形式输出要运行的对应函数和你给我的回复\n",
    "你只需要回答一个包含数字的列表即可，不要回答任何中文\n",
    "\n",
    "归零回到原点：0\n",
    "点头：1\n",
    "摇头：2\n",
    "跳舞：3\n",
    "\n",
    "【以下是一些具体的例子】\n",
    "我的指令：请跳支舞吧。你回复：[3]\n",
    "我的指令：先回到原点，再摇摇头。你回复：[0, 2]\n",
    "我的指令：先点点头，然后再跳支舞吧。你回复：[1, 3]\n",
    "我的指令：归零，点个头，最后跳支舞。你回复：[0, 1, 3]\n",
    "\n",
    "【我现在的指令是】\n",
    "{}\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33475538-c2f8-4008-a3b9-c0131ded6771",
   "metadata": {},
   "source": [
    "## 大模型对话函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c1775edc-50ee-49e4-958c-6317d23d091b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def robot_control(order='回到原点吧'):\n",
    "    result = phi35_ask(robot_order_template.format(order))\n",
    "    return result.split('\\n')[-1].strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d8fd6f09-d570-469f-aa94-5b3eedde776c",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = robot_control('先跳个舞，给我点个头，最后回到零位')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "dd1ab6e6-5295-43d0-80f9-7b40366d1861",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[3, 1, 0]'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "837f2a0c-35c1-4543-bc52-183748128dc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = robot_control('做一个拒绝我的动作')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "164dcd24-1f07-4f42-92d5-f8cd9ef373cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[2]'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ab393408-3157-43f1-a109-da2c3a91899b",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = robot_control('做一个欢迎我的动作')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f5c24148-f2f4-406f-99cd-46e0ea6ada54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[1, 3]'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b3936cc-8376-4893-bc6b-e607022735f7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
